<?
	chdir("..");
	include("include/common.php");
	include("siteadmin/accesscontrol.php");
	include("siteadmin/header.php");
?>
<?
		$n1 = $n2 = 0;
		if ( preg_match("/delete/i", $_POST['change3']) ){
			$qr1 = mysql_query("SELECT * FROM cats");
			$max = 0;
			while ($d = mysql_fetch_object($qr1)){
				$key = $_POST["delete{$d->id}"];
				if($key){
					if($d->name == $key){
						$id=$d->id;
						@mysql_query("DELETE FROM cats WHERE id=$id");
						echo "{$d->name} was deleted<bR>";
						$uC++;
					}
				}
			}
		}
		if ($_POST['change3']){
			$qr1 = mysql_query("SELECT * FROM cats");
			$max = 0;
			while ($a = mysql_fetch_object($qr1)){
				$x[$a->id] = 1;
				if ($a->id > $max) $max = $a->id;
				if (preg_match("/update/i", $_POST['change3'])){
					$query = "UPDATE cats SET 
					name='".addslashes($_POST["name$a->id"])."',
					parent='".addslashes($_POST["parent$a->id"])."',
					status='".addslashes($_POST["status$a->id"])."' WHERE id=$a->id";
					mysql_query($query) or die( mysql_error()."<br>$query<br>" );
				}
			}
			for ($i = 0; $i <= $max; $i++){
				if (!$x[$i]) break;
				$sel_id = $i;
			}
		}
		if (preg_match("/add/i", $_POST['change8']) && $_POST['name_new'] != ''){
			$query = "INSERT INTO cats SET name='".addslashes($_POST["name_new"])."',parent='".addslashes($_POST["parent_new"])."',status='1'";
			mysql_query($query) or die( mysql_error()."<BR>$query<br>");
		}
	?>
	<!------///////////////--->
	<?=$sitename?> Categories
	<!------///////////////--->
	<TABLE class=design  cellPadding=3 cellSpacing=0 width=100% border='1' BORDERCOLOR="#ffffff" STYLE="border-collapse: collapse">
	<tr>
		<TD class="a1"  style="padding-left:10px;">&nbsp;</td>
		<TD class="a1">
			<TABLE class=design  cellPadding=3 cellSpacing=0 width=100% border='1' BORDERCOLOR="#ffffff" STYLE="border-collapse: collapse">
			<FORM name=form1 method=post>
	<?
			$qr1 = mysql_query("SELECT * FROM cats ORDER BY name ASC");
			$qr2 = mysql_query("SELECT * FROM cats ORDER BY name ASC");
			$itotal = 0;
			for ($i = mysql_num_rows($qr2) - 1; $i >= 0; $i--){
	//			if (mysql_result($qr2, $i, 'name')){
					$itotal++;
	//			}
			}
			$i = 1;
			while ($a = mysql_fetch_object($qr1)){
				if ( $a->parent ){
					$hdr = getCat($a->parent);
				}
				if ($hdr){
					$name = $hdr." - ".$a->name;
				}else{
					$name = $a->name;
				}
				$kname = $a->name;
	?>
	<TR>
		<TD class="a1" width=15><input type=checkbox name="delete<?=$a->id?>" value="<?=$kname?>">
		<TD class="a1" width=15><?=$i?></TD> 
		<TD class="a1"><input type=text name=name<?=$a->id?> size=50 value="<?=htmlspecialchars($a->name)?>"></TD>
		<TD class="a1"><? buildCatBox("parent".$a->id,$a->parent) ?></td>
		<TD class="a1">
			<select name=status<?=$a->id?>>
				<option value="0" <?if($a->status == "0")echo "SELECTED";?>>Inactive
				<option value="1" <?if($a->status == "1")echo "SELECTED";?>>Active
			</select>
		</TD>
			<input type="hidden" name="parent<?=$a->id?>" value="<?=$a->parent?>">
			<input type="hidden" name="akey<?=$a->id?>" value="<?=$a->akey?>">
	<?
	//			$itotal = $i;
				$i++;
			}
	?>
		<input type="hidden" name="itotal" value="<?=$itotal?>">
	<TR><TH colspan=4><P align=center><input type=submit name=change3 value="Update">
		<input type=submit name=change3 value="Delete selected" <?=$del_confirm?>></TH></TR>
		<INPUT type=hidden name=up>
		<INPUT type=hidden name=down>
		<input type="hidden" name="nextid" value="<?if(!$itotal){$nextid=0;}else{$nextid=$itotal;}echo $nextid;?>">
		<input type="hidden" name="asuid" value="<?=$asuid;?>">
	<TR>
		<TD class="a1" width=15>&nbsp;</TD>
		<TD class="a1"><?=$i?></TD>
		<TD class="a1" ><input type=text name=name_new size=30></TD>
		<TD class="a1" ><? buildCatBox("parent_new") ?></td>
		<TD class="a1">
			<select name=status_new>
				<option value="0">Inactive
				<option value="1">Active
			</select>
		</TD>
	</TR>
	<TR><TD class="a1" colspan=4><input type=submit name=change8 value="Add new record"></TH></TR>
	</FORM>
	</TABLE>
	</td></tr>
	</table>
	</CENTER>
<?
	include("siteadmin/footer.php");
?>
<?
	function buildCatBox($name,$sel=""){
?>
		<SELECT NAME="<?=$name?>">
			<option value="">-----
<?
			$qr1 = mysql_query("SELECT * FROM cats ORDER BY name ASC");
			while ($a = mysql_fetch_object($qr1)){
				if ( $a->parent ){
					$hdr = getCat($a->parent);
				}
				if ($hdr){
					$a->name = $hdr." - ".$a->name;
				}else{
					$a->name = $a->name;
				}
				if($sel == $a->id){
					echo "<OPTION VALUE=\"{$a->id}\" SELECTED>{$a->name}</OPTION>\n"; 
				}else{
					echo "<OPTION VALUE=\"{$a->id}\">{$a->name}</OPTION>\n"; 
				}
			}
?>
		</select>
<?
	}
	function getCat($cat){
		$qr1 = mysql_query("SELECT name FROM cats WHERE id='$cat'");
		$a = mysql_fetch_object($qr1);
		return $a->name;
	}
?>